package com.camel.docker.service.impl;

import com.camel.docker.config.FTPConfig;
import com.camel.docker.service.DockerClientService;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.DockerCmdExecFactory;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DockerClientServiceImpl implements DockerClientService {

    @Autowired
    private FTPConfig ftpConfig;

    @Override
    public DockerClient connect() {
        DockerClientConfig custom = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("tcp://" + ftpConfig.getHost() + ":2375")
                .withDockerTlsVerify(false).build();
        DockerCmdExecFactory factory = this.factory();
        DockerClient dockerClient = DockerClientBuilder.getInstance(custom).withDockerCmdExecFactory(factory).build();
        return dockerClient;
    }

    @Override
    public DockerCmdExecFactory factory() {
        DockerCmdExecFactory dockerCmdExecFactory =  new JerseyDockerCmdExecFactory()
                .withReadTimeout(30000)
                .withConnectTimeout(30000)
                .withMaxTotalConnections(100)
                .withMaxPerRouteConnections(10);
        return dockerCmdExecFactory;
    }
}
